<?php
namespace app\api\controller;


use think\Db;
use app\api\model\Banner as  BannerModel;
use app\api\model\Search  as  SearchModel;
use app\api\model\Mechanism as MechanismModel;
use app\api\model\InstitutionalActivities as InstitutionalActivitiesModel;
use app\api\model\User  as  UserModel;
class Index  extends Common {

    /***
     * 首页Icon
     */
    public function Icon(){
        $res = Db::table('up_Icon')->where('status',1)->value('img');
        $this->apiSuccess($res);
    }

    /***
     * 首页Banner
     */
    public function Banner(){
        $BannerModel = new BannerModel();

        $res = $BannerModel->selectBanner();
        $this->apiSuccess($res);
    }

    /***
     * Banner详情
     */
    public function BannerDetail(){
         $banner_id = input('param.Banner_id');
         $BannerModel = new BannerModel();
         $field='banner_id,banner_img,banner_title,banner_content';
         $res = $BannerModel->selectBannerOne($field,$banner_id);
         return json_encode(['status' => 200, 'msg' => '成功', 'data' => $res],true);
    }

    /***
     * 搜索接口
     */
    public function Search(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
//        $user_id = 45;
        $MechanismModel = new MechanismModel();
        $InstitutionalActivitiesModel = new InstitutionalActivitiesModel();
        $type = input('param.type');//1.活动   2.机构
        $keyword = input('param.keyword');
        $page = input('param.page')?input('param.page'):1;
        if($type==1){
            $list = $InstitutionalActivitiesModel->getActivitys('','*',$page,2,$keyword,20);
        }elseif ($type==2){
            $list = $MechanismModel->getMechanismList($keyword,$page,2);
        }
        if($user_id!=0){
            $whereMap['search_user_id'] = $user_id;
            $whereMap['search_content'] = $keyword;
            $whereMap['search_type'] = $type;
            $res = Db::table('up_user_search')->where($whereMap)->value('search_id');
            if(!$res){//没有查到就插入用户搜索记录表
                $data['search_content'] = $keyword;
                $data['search_type'] = $type;
                $data['search_user_id'] = $user_id;
                $data['search_datetime'] = date('Y-m-d H:i:s',time());
                Db::table('up_user_search')->insert($data);
            }else{
                $data['search_datetime'] = date('Y-m-d H:i:s',time());
                Db::table('up_user_search')->where('search_id',$res)->update($data);
            }
        }

         if($list){
             $this->apiSuccess($list);
         }else{
            $this->apiError('L305');
        }
    }
    /**
     * 用户的历史搜索记录
     */
    public function SearchList(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
        $SearchModel = new SearchModel();
        $type = input('param.type');
        $list = $SearchModel->History($user_id,$type);
        $this->apiSuccess($list);
    }
    /***
     * 清除用户的搜索历史记录
     */
    public function Clear(){
        $SearchModel = new SearchModel();
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
        $type = input('param.type');
        if($type==1){
            $where['search_id'] = input('param.search_id');
            $list = $SearchModel->clear($where);
            if($list){
                $this->apiSuccess();
            }else{
                $this->apiError('E001');
            }
        }elseif ($type==2){
            $type_lx = input('param.type_lx');
            $where['search_user_id'] = $user_id;
            $where['search_type'] = $type_lx;
            $count = Db::table('up_user_search')->where($where)->count();
            if($count>0){
                $list = $SearchModel->clearAll($user_id,$type_lx);
                if($list){
                    $this->apiSuccess();
                }else{
                    $this->apiError('E001');
                }
            }else{
                $this->apiError(array('msg' => "没有数据不能删除", 'status' => 'L200'));
            }
        }

    }
    /***
     * 活动分类
     */
    public function Activity(){
        $res = Db::table('up_activity')->where('id','NEQ',1)->select();
        $this->apiSuccess($res);
    }

    /**
     * 机构活动列表
     */
    public function ActivityList(){
        $now_time   = date("Y-m-d H:i:s", time());
        $after_time = date("Y-m-d H:i:s", strtotime("+1 week"));

        Db::table('up_institutional_activities')
            ->where('activity_start_time','>',$now_time)
            ->setField('is_gq',2);
        $InstitutionalActivitiesModel = new InstitutionalActivitiesModel();
        $type = input('param.type')?input('param.type'):1;
        $page = input('param.page')?input('param.page'):1;
        $id = input('param.id');
        $where['activity_sh'] = 1;
        $where['is_show'] = 1;
        $field = 'a.activity_status,a.activity_address,a.id,a.activity_name,a.activity_img,a.activity_start_time,a.activity_end_time,a.activity_id';
        if($type==1){//热门
            $where['activity_status'] = 2;
            $where['activity_start_time'] = array('between',array($now_time,$after_time));
            $list = $InstitutionalActivitiesModel->selectList($where,'desc',$page,$field);
        }elseif ($type==2){//后台配置的
            $id = ','.$id.',';
            $where['activity_id'] = array('like',"%$id%");
            $where['activity_status'] = array('IN','1,2,3,5');
            $list = $InstitutionalActivitiesModel->selectList($where,'asc',$page,$field,2);
        }
        return $this->apiSuccess($list);
    }


    /***
     * 活动详情
     */
    public function ActivityDetail(){
        header("Access-Control-Allow-Origin:*"); //允许跨域请求

        extract(input());
        if($user_id != 0){
            $auth = $this->checkAll($user_id,$time,$value,$reqKey);
            if($auth == false){
                $this->apiError(301);
            }
        }
        $page = input('param.page')?input('param.page'):1;
        $id = input('param.id');//活动ID
        $where['user_id'] = $user_id;
        $where1['a.id'] = $id;
        //这条活动的信息
        $list = Db::table('up_institutional_activities')->alias('a')
            ->join('up_mechanism um','um.mechanism_id = a.activity_mechanism_id','left')
            ->where($where1)->field('a.*,um.mechanism_name')->find();
        $activity_name =Db::table('up_activity')->where('id','IN',trim($list['activity_id'],','))->column('activity_name');
        $list['name'] = implode(',',$activity_name);
        $where['institutional_activities_id'] = $id;
        $my_activities = Db::table('up_my_activities')->where($where)->value('id');//查询该用户该活动是否已经收藏

        //查询该用户该活动是否已经参加这个活动
        $my_activities_member = Db::table('up_institutional_activities_member')->where($where)->find();
        $now_time = time();//报名的现在时间
        if(strtotime($list['activity_sign_start'])>0&&strtotime($list['activity_sign_end'])>0){
            //设置活动报名的时间
            if(strtotime($list['activity_sign_start'])<$now_time && $now_time <strtotime($list['activity_sign_end'])-7200){

                $my_activities_member2 = Db::table('up_institutional_activities_member')->where($where)->find();
                $list['already_sign'] = $my_activities_member2['sign_status'];
            }else{
                if(strtotime($list['activity_sign_end'])<$now_time&&
                    $now_time<strtotime($list['activity_start_time'])-7200){
                    $list['already_sign'] = 5;//活动的报名结束时间到活动的开始时间的空白期
                }else{
                    if(strtotime($list['activity_start_time'])-7200<$now_time&&$now_time<strtotime($list['activity_end_time'])){
                        if($my_activities_member['sign_status'] ==1){
                            $whereMap['user_id'] = $user_id;
                            $whereMap['institutional_activities_id'] = $id;
                            Db::table('up_institutional_activities_member')->where($whereMap)->setField('sign_status',2);
                        }

                        $my_activities_member2 = Db::table('up_institutional_activities_member')->where($where)->find();
                        $list['already_sign'] = $my_activities_member2['sign_status'];
                    }else{
                        $my_activities_member2 = Db::table('up_institutional_activities_member')->where($where)->find();
                        $list['already_sign'] = $my_activities_member2['sign_status'];
                    }

                }
            }
        }else{//没有设置活动报名时间
            if(strtotime($list['create_time'])<$now_time&&$now_time <strtotime($list['activity_end_time'])){
                if($my_activities_member['sign_status']==1){
                    $whereMap['user_id'] = $user_id;
                    $whereMap['institutional_activities_id'] = $id;
                    Db::table('up_institutional_activities_member')->where($whereMap)->setField('sign_status',2);
                    $my_activities_member2 = Db::table('up_institutional_activities_member')->where($where)->find();
                    $list['already_sign'] = $my_activities_member2['sign_status'];
                }else{
                    $my_activities_member2 = Db::table('up_institutional_activities_member')->where($where)->find();
                    $list['already_sign'] = $my_activities_member2['sign_status'];
//                    $list['already_sign'] = $my_activities_member['sign_status'];
                }
            }
        }
        if($my_activities_member['id']==''){//该用户该活动的么有报名状态
            $list['already_sign'] = 4;
            $list['sign_time_member'] ='';
            $list['sign_end_time_member'] ='';
        }else{
            $list['sign_time_member'] =$my_activities_member['sign_time'];
            $list['sign_end_time_member'] =$my_activities_member['sign_end_time'];
        }
        if($list['activity_status']==3){//进行中查询该活动的评论
                $num = $page-1;//分页
                $res = Db::table('up_institutional_activities_comment')//评论
                            ->alias('a')
                            ->join('up_u_user uuu','uuu.user_id = a.answer_user_id','left')
                            ->where('activities_id',$list['id'])
                            ->field('a.*,uuu.user_username,uuu.user_image')
                            ->limit($num*10,10)
                            ->select();
            $count = Db::table('up_institutional_activities_comment')//评论
            ->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.answer_user_id','left')
                ->where('activities_id',$list['id'])
                ->count();
            $list['count'] = $count;
                foreach ($res as $vk=>&$vv){//评论点赞
                    $where2['data_id'] = $vv['answer_id'];
                    $where2['type'] = 1;
                    $where2['user_id'] = $user_id;
                    $dz_id = Db::table('up_dz')->where($where2)->value('dz_id');
                    if($dz_id){//证明点赞过了
                        $vv['dz'] = 1;
                    }else{//没有点赞
                        $vv['dz'] = 2;
                    }
                    $count_answer = Db::table('up_institutional_activities_answer')
                        ->alias('a')
                        ->join('up_u_user uuu','uuu.user_id = a.reply_user_id','left')
                        ->where('a.reply_information_answer_id',$vv['answer_id'])
                        ->count();
                    $vv['count_answer'] = $count_answer;
                    $aa = explode(',',trim($vv['answer_img'],','));
                    if(count(array_filter($aa))>0){
                        $vv['answer_img'] = $aa;
                    }else{
                        $vv['answer_img'] = array();
                    }
                }
                foreach ($res as $key=>$vo){//对评论进行回复
                    $a= Db::table('up_institutional_activities_answer')
                        ->alias('a')
                        ->join('up_u_user uuu','uuu.user_id = a.reply_user_id','left')
                        ->where('a.reply_information_answer_id',$vo['answer_id'])
                        ->limit(2)
                        ->field('a.*,uuu.user_username')
                        ->order('a.reply_create_time desc')
                        ->select();
                    $res[$key]['answer'] = $a;
                }
                $list['first_answer'] = $res;//回复评论
        }
        if($my_activities){
            $list['collection'] = 1;//证明已经收藏了
        }else{
            $list['collection'] = 2;//证明没有收藏了
        }
        $list['time'] = time();
        return json_encode(['status' => 200, 'msg' => '成功', 'data' => $list],true);

    }

    /**
     * 收藏活动
     */
    public function Collection(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
        $id = input('param.id');
        $where['user_id'] = $user_id;
        $where['institutional_activities_id'] = $id;
        $my_activities = Db::table('up_my_activities')->where($where)->value('id');
        if($my_activities){
            $res = Db::table('up_my_activities')->where($where)->delete();

        }else{
            $data['user_id'] = $user_id;
            $data['institutional_activities_id'] = $id;
            $data['create_time'] = date('Y-m-d H:i:s',time());
            $res = Db::table('up_my_activities')->insertGetId($data);

        }

        if($res){
            $this->apiSuccess();
        }else{
            $this->apiError('E000');
        }
    }

    /***
     * 取消收藏
     */
//    public function Cancel(){
//        extract(input());
//        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
//        if($auth == false){
//            $this->apiError(301);
//        }
//        $id = input('param.id');//活动ID
//        $where['user_id'] = $user_id;//用户ID
//        $where['institutional_activities_id'] = $id;
//        $activities_id = Db::table('up_my_activities')->where($where)->value('id');//查询这个用户是否收藏活动
//        if(!$activities_id){
//            $this->apiError('E125');
//        }else{
//            $res = Db::table('up_my_activities')->where($where)->delete();
//            if($res){
//                $this->apiSuccess();
//            }else{
//                $this->apiError('E001');
//            }
//        }
//
//    }

    public function nextbm(){
        $userModel  = new UserModel();
        $InstitutionalActivitiesModel=new InstitutionalActivitiesModel();
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
        $id = input('param.id');
        $count = Db::table('up_institutional_activities_member')->where('institutional_activities_id',$id)->count();
        $list = Db::table('up_institutional_activities')->where('id',$id)->field('enrolment,rang_enrolment')->find();
        if($list['enrolment']==2){
            if($count>=$list['rang_enrolment']){
                $this->apiError('E137');
            }
        }
        $field = 'user_id,user_username,user_phone,user_attestation,user_post,user_to_mechanism_id,user_to_mechanism_id';
        $res = $userModel->selectUserInfo($user_id,$field);
        if($res['user_to_mechanism_id']==-1){
            $this->apiError('E135');
        }
        $field = 'user_id,user_username,user_phone,user_attestation,user_post,user_to_mechanism_id,user_to_mechanism_id';
        $res = $userModel->selectUserInfo($user_id,$field);
        if($res['user_to_mechanism_id']==-1){
            $this->apiError('E135');
        }
        if($res['user_attestation']!=3){
            $this->apiError('E138');
        }
        $now_time = time();//报名的现在时间

        $where['id'] = $id;
        $info = $InstitutionalActivitiesModel->getInfoList($where);//获取单条活动信息
        if(strtotime($info['activity_sign_start'])>0&&strtotime($info['activity_sign_end'])>0){//设置活动报名的时间
            if(strtotime($info['activity_sign_start'])<$now_time
                &&$now_time <strtotime($info['activity_sign_end'])){//时间段之内
                $this->thisMethods($user_id,$id);
            }else{
                $this->apiError('K101');
            }
        }else{//没有设置活动报名时间
            if(strtotime($info['create_time'])<$now_time
                &&$now_time <strtotime($info['activity_end_time'])){
                $this->thisMethods($user_id,$id);
            }else{
                $this->apiError('K101');
            }
        }
    }
    /***
     * @param $user_id
     * @param $id
     * 报名
     */
    protected function thisMethods($user_id,$id){
        $data['user_id'] = $user_id;
        $data['institutional_activities_id'] = $id;
        $member_id = Db::table('up_institutional_activities_member')->where($data)->value('id');//查询该用户是否已经报名
        if($member_id){
            $this->apiError('E122');
        }else{
            $this->apiSuccess();
        }
    }
    /***
     * 报名回显
     */
    public function indication()
    {
        $userModel  = new UserModel();
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
        $id = input('param.id');
        $count = Db::table('up_institutional_activities_member')->where('institutional_activities_id',$id)->count();
        $list = Db::table('up_institutional_activities')->where('id',$id)->field('enrolment,rang_enrolment')->find();
        if($list['enrolment']==2){
            if($count>=$list['rang_enrolment']){
                $this->apiError('E137');
            }
        }
        $field = 'user_id,user_username,user_phone,user_attestation,user_post,user_to_mechanism_id,user_to_mechanism_id';
        $res = $userModel->selectUserInfo($user_id,$field);
        if($res['user_to_mechanism_id']==-1){
            $this->apiError('E135');
        }

        //TODO  此处要修改
        if($res['user_attestation']==3){
            //查询职务
            $assortment_name =Db::table('up_assortment')->where('assortment_id',$res['user_post'])->value('assortment_name');
            $mechanism_name =Db::table('up_mechanism')->where('mechanism_id',$res['user_to_mechanism_id'])->value('mechanism_name');
            $res['assortment_name'] = $assortment_name;
            $res['mechanism_name'] = $mechanism_name;
        }
        $this->apiSuccess($res);
    }

    /***
     * 提交报名
     */
    public function postSign(){
        $userModel  = new UserModel();

        $InstitutionalActivitiesModel=new InstitutionalActivitiesModel();
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
        $id = input('param.id');

        $field = 'user_id,user_username,user_phone,user_attestation,user_post,user_to_mechanism_id,user_to_mechanism_id';
        $res = $userModel->selectUserInfo($user_id,$field);
        if($res['user_to_mechanism_id']==-1){//没有加入机构的用户不能报名
            $this->apiError('E135');
        }
        if($res['user_attestation']!=3){//没有认证成功的用户不能报名
            $this->apiError('E135');
        }
        $now_time = time();//报名的现在时间

        $where['id'] = $id;
        $info = $InstitutionalActivitiesModel->getInfoList($where);//获取单条活动信息
        if(strtotime($info['activity_sign_start'])>0&&strtotime($info['activity_sign_end'])>0){//设置活动报名的时间
            if(strtotime($info['activity_sign_start'])<$now_time
                  &&$now_time <strtotime($info['activity_sign_end'])){//时间段之内
                    $this->thisMethod($user_id,$id,$info['activity_name'],$info['activity_mechanism_id']);
              }else{
                  $this->apiError('K101');
              }
        }else{//没有设置活动报名时间
           if(strtotime($info['create_time'])<$now_time
               &&$now_time <strtotime($info['activity_end_time'])){
               $this->thisMethod($user_id,$id,$info['activity_name'],$info['activity_mechanism_id']);
           }else{
               $this->apiError('K101');
           }
        }
    }

    /***
     * @param $user_id
     * @param $id
     * 报名
     */
    protected function thisMethod($user_id,$id,$activity_name,$activity_mechanism_id){
        $InstitutionalActivitiesModel=new InstitutionalActivitiesModel();
        $data['user_id'] = $user_id;
        $data['institutional_activities_id'] = $id;
        $member_id = Db::table('up_institutional_activities_member')->where($data)->value('id');//查询该用户是否已经报名
        if($member_id){
            $this->apiError('E122');
        }else{
            $res = Db::table('up_institutional_activities_member')->insertGetId($data);//插入活动报名表
            $InstitutionalActivitiesModel->where('id',$id)->setInc('activity_num',1);//报名人数每次加1
            if($res){
                $whereMap['n_type'] = 4;
                $whereMap['n_activitie'] = $id;
                $resList = Db::table('up_notice')->where($whereMap)->find();//查询这个消息是否已经添加
                if(!$resList['n_id']){//证明没有添加消息
                    $datas['n_createtime'] =date('Y-m-d H:i:s',time());
                    $datas['n_title'] ="您报名的".$activity_name."报名成功";
                    $datas['n_read_user'] =',0,';
                    $datas['n_clear_user'] =',0,';
                    $datas['n_user_id'] =',0,'.$user_id.',';
                    $datas['n_activitie'] =$id;
                    $datas['n_mechanism'] =$activity_mechanism_id;
                    $datas['n_type'] =2;
                    //插入消息表里面
                    Db::table('up_notice')->insert($datas);
                    //todo  缺少极光推送
                    $u_id = (string)$user_id;
                    $msg = '您参加的' . $activity_name . '报名成功';
                    $this->Jpush(1, $u_id, '活动报名成功', ['type'=>1], $msg, 2);
                }else{//添加了
                        $uid = ','.$user_id.',';
                        if(strpos($resList['n_user_id'],$uid) !==false){

                        }else{
                            $user_id_i = $user_id;
                            $str = $resList['n_user_id'].$user_id_i.',';
                            //更新发送
                            Db::table('up_notice')->where('n_id',$resList['n_id'])->setField('n_user_id',$str);
                        }
                    //todo 缺少极光推送
                    $u_id = (string)$user_id;
                    $msg = '您参加的' . $activity_name . '报名成功';
                    $this->Jpush(1, $u_id, '活动报名成功', ['type'=>1], $msg, 2);
                }
                $this->apiSuccess();
            }else{
                $this->apiError('K100');
            }
        }
    }
    /***
     * 取消报名
     */
    public function cancelSign(){
        $InstitutionalActivitiesModel=new InstitutionalActivitiesModel();
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
        $id = input('param.id');//活动ID
        $where1['user_id'] = $user_id;
        $where1['institutional_activities_id'] = $id;
        //查询这个用户的报名这个活动
        $sign_status_info = Db::table('up_institutional_activities_member')->where($where1)->find();
        if(!$sign_status_info['id']){//证明没有报名，不能取消报名
            $this->apiError('E123');
        }
//        else{
//            if($sign_status_info['sign_status']==2){//已经签到不能取消报名
//                $this->apiError('E129');
//            }
//        }
        $now_time = time();//取消报名的现在时间
        $where['id'] = $id;
        $info = $InstitutionalActivitiesModel->getInfoList($where);//获取单条活动信息
        if($info['activity_status']==3){
            $this->apiError('E124');
        }
        if(strtotime($info['activity_sign_start'])>0&&strtotime($info['activity_sign_end'])>0){
            //设置活动报名的时间
            if(strtotime($info['activity_sign_start'])<$now_time
                &&$now_time <strtotime($info['activity_sign_end']))
            {//时间段之内
                $this->thisSign($user_id,$id);//取消报名

            }else{
//                if(strtotime($info['activity_sign_end'])<=strtotime($info['activity_start_time'])){//活动报名时间和活动开始时间相等
//                    $time = strtotime($info['activity_start_time'])-7200;//前2个小时
//                    if($time<$now_time&&$now_time<strtotime($info['activity_start_time'])){//在这个时间段里面不可以取消报名
                        //不可以取消报名
                        $this->apiError('E124');
//                    }else{
//                        //取消报名
//                        $this->thisSign($user_id,$id);
//                    }
//                }
            }
        }else{//没有设置活动报名时间
            if(strtotime($info['create_time'])<$now_time &&$now_time <strtotime($info['activity_end_time'])){
                //可以取消报名
                $this->thisSign($user_id,$id);
            }else{//不可以取消取消报名
                $this->apiError('E124');
            }
        }
    }

    /**
     * @param $user_id
     * @param $id
     * 取消报名
     */
    protected function thisSign($user_id,$id){
        $InstitutionalActivitiesModel=new InstitutionalActivitiesModel();
        $data['user_id'] = $user_id;
        $data['institutional_activities_id'] = $id;
        $member_id = Db::table('up_institutional_activities_member')->where($data)->value('id');//查询该用户是否已经报名
        if(!$member_id){
            $this->apiError('E123');
        }else{
            $res = $InstitutionalActivitiesModel->where('id',$id)->setDec('activity_num',1);//报名人数每次减1
             Db::table('up_institutional_activities_member')->where($data)->delete();//删除该用户报名

            if($res){
                $this->apiSuccess();
            }else{
                $this->apiError('K100');
            }
        }
    }

    /***
     * 签到
     */
    public function SignIn(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
        $now_time = time();//签到时间
        $id = input('param.id');//活动ID
        $where['institutional_activities_id'] = $id;//活动ID
        $where['user_id'] = $user_id;//用户ID
        $activity_start_time = Db::table('up_institutional_activities')//查询这个活动的开始时间
                    ->where('id',$id)
                    ->value('activity_start_time');
        $sign_time = strtotime($activity_start_time) - 7200;//签到的前2个小时
        if($sign_time<$now_time&&$sign_time<strtotime($activity_start_time)){//在这个时间段才可以签到

            $res = Db::table('up_institutional_activities_member')
                ->where($where)
                ->find();
            if(!$res['id']){
                $this->apiError('E123');
            }else{
//                if($res['sign_status']==2){//证明已经签到中
                  //签到更新数据库字段
                    $data['sign_time'] = date('Y-m-d H:i:s',time());
                    $data['sign_status'] = 3;
                    $data['sign_longitude'] =input('param.sign_longitude');
                    $data['sign_latitude'] =input('param.sign_latitude');
                    $whereMap['user_id'] = $user_id;
                    $whereMap['institutional_activities_id'] = $id;
                    $list = Db::table('up_institutional_activities_member')->where($whereMap)->update($data);

                    if($list){
                        $this->apiSuccess();
                    }else{
                        $this->apiError('E127');
                    }
//                }
            }
        }else{
                $this->apiError('E128');
        }
    }

    /**
     * 查询签到的时间信息
     */
    public function SignInfo(){
        $id = input('param.id');//活动ID
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
        $where['user_id'] = $user_id;
        $where['institutional_activities_id'] = $id;
        $member_info = Db::table('up_institutional_activities_member')->where($where)->find();//查询用户参加活动信息
        $this->apiSuccess($member_info);

    }
    /***
     * 签退
     */
    public function SignBack(){
        $id = input('param.id');//活动ID
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }

        $now_time = time();//签退时间
        $where['user_id'] = $user_id;
        $where['institutional_activities_id'] = $id;
        $res = Db::table('up_institutional_activities_member')->where($where)->find();
        if(!$res['id']){
            $this->apiError('E123');
        }else{
               $activity_end_time= Db::table('up_institutional_activities')->where('id',$id)->field('activity_end_time')->find();
//                if($res['sign_status']==3){
                    if($now_time>strtotime($activity_end_time['activity_end_time'])){
                        $this->apiError('E130');
                    }else{
                        $data['sign_status']  =3;
                        $data['sign_end_time'] = date('Y-m-d H:i:s',time());
                        Db::table('up_institutional_activities_member')->where($where)->update($data);
                        $this->apiSuccess();

                    }
//                }
        }
    }

    /***
     * 发布评论
     */
    public function ReleaseComment(){
        $img = input('param.comment_img');//图片
        $content = input('param.content');//内容
        $id = input('param.id');//活动ID
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
        $data['answer_user_id'] =$user_id;
        $data['answer_content'] =$content;
        $data['activities_id'] =$id;
        $data['answer_img'] =$img;
        $data['answer_time'] =date('Y-m-d H:i:s',time());
        $res = Db::table('up_institutional_activities_comment')->insertGetId($data);
        if($res){
            $this->apiSuccess();
        }else{
            $this->apiError('K100');
        }
    }
    /***
     * 查看更多评论回复
     */
    public function MoreReply(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
        $page = input('param.page')?input('param.page'):1;
        $num = $page - 1;
        $answer_id = input('param.answer_id');
        $res = Db::table('up_institutional_activities_comment')//评论
            ->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.answer_user_id','left')
            ->where('answer_id',$answer_id)
            ->field('a.*,uuu.user_username,uuu.user_image')
            ->find();
        $where2['data_id'] = $res['answer_id'];
        $where2['type'] = 1;
        $where2['user_id'] = $user_id;
        $dz_ids = Db::table('up_dz')->where($where2)->value('dz_id');
        if($dz_ids){//证明点赞过了
            $res['dz'] = 1;
        }else{//没有点赞
            $res['dz'] = 2;
        }
        $aa = explode(',',trim($res['answer_img'],','));
        if(count(array_filter($aa))>0){
            $res['answer_img'] = $aa;
        }else{
            $res['answer_img'] = array();
        }
        $count= Db::table('up_institutional_activities_answer')
            ->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.reply_user_id','left')
            ->where('a.reply_information_answer_id',$res['answer_id'])
            ->count();
        $res['count'] = $count;
        $a= Db::table('up_institutional_activities_answer')
            ->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.reply_user_id','left')
            ->where('a.reply_information_answer_id',$res['answer_id'])
            ->field('a.*,uuu.user_username,uuu.user_image')
            ->order('a.reply_create_time desc')
            ->limit($num*10,10)
            ->select();
        foreach($a as $key=>&$vo){
            $where['type'] = 2;
            $where['user_id'] = $user_id;
            $where['data_id'] = $vo['reply_id'];
            $dz_id = Db::table('up_dz')->where($where)->value('dz_id');
            if($dz_id){//证明点赞过了
                $vo['dz'] = 1;
            }else{//没有点赞
                $vo['dz'] = 2;
            }
//            $aa = explode(',',trim($vo['answer_img'],','));
//            if(count(array_filter($aa))>0){
//                $vo['answer_img'] =$aa;
//            }else{
//                $vo['answer_img'] =array();
//            }
        }
        $res['more'] = $a;
        $this->apiSuccess($res);die;
    }
    /***
     * 评论回复
     */
    public function Reply(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
        $answer_id = input('param.answer_id');
        $content  = input('param.content');
        $data['reply_user_id'] = $user_id;
        $data['reply_information_answer_id'] = $answer_id;
        $data['reply_content'] = $content;
        $data['reply_create_time'] = date('Y-m-d H:i:s',time());
        $res = Db::table('up_institutional_activities_answer')->insertGetId($data);
        if($res){
            $this->apiSuccess();
        }else{
            $this->apiError('E000');
        }
    }
    /***
     * 评论点赞
     */
    public function CommentDz(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
        $answer_id = input('param.answer_id');
        $type = input('param.type');
        $where['user_id'] = $user_id;
        $where['type'] = $type;
        $where['data_id'] = $answer_id;
        $dz_id = Db::table('up_dz')->where($where)->value('dz_id');//查询这个用户是否已经点赞
        if($type==1){//评论点赞
            if($dz_id){//已经点赞就取消点赞
                $res = Db::table('up_institutional_activities_comment')->where('answer_id',$answer_id)->setDec('answer_dz',1);
                $list = Db::table('up_dz')->where($where)->delete();
            }else{//点赞插入数据
                $res = Db::table('up_institutional_activities_comment')->where('answer_id',$answer_id)->setInc('answer_dz',1);
                $data['user_id'] = $user_id;
                $data['type'] = 1;
                $data['data_id'] = $answer_id;
                $list = Db::table('up_dz')->insertGetId($data);
            }
            if($list&&$res){
                $this->apiSuccess();
            }else{
                $this->apiError('E133');
            }
        }elseif ($type==2){//评论回复点赞
            if($dz_id){//已经点赞就取消点赞
                $res = Db::table('up_institutional_activities_answer')->where('reply_id',$answer_id)->setDec('reply_thumbs_up',1);
                $list = Db::table('up_dz')->where($where)->delete();
            }else{//点赞插入数据
                $res = Db::table('up_institutional_activities_answer')->where('reply_id',$answer_id)->setInc('reply_thumbs_up',1);
                $data['user_id'] = $user_id;
                $data['type'] = 2;
                $data['data_id'] = $answer_id;
                $list = Db::table('up_dz')->insertGetId($data);
            }
            if($list&&$res){
                $this->apiSuccess();
            }else{
                $this->apiError('E133');
            }
        }
    }

    /***
     * 删除评论
     */
    public function DeleteComment(){
        extract(input());

        $answer_id = input('param.answer_id');
        $type = input('param.type');
        Db::startTrans();
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
        if($type==1){//评论删除
            $answer_user_id = Db::table('up_institutional_activities_comment')->where('answer_id',$answer_id)->value('answer_user_id');
            if($answer_user_id!=$user_id){
                $this->apiError('E134');
            }
            $res = Db::table('up_institutional_activities_comment')->where('answer_id',$answer_id)->delete();
            $reply_id =Db::table('up_institutional_activities_answer')->where('reply_information_answer_id',$answer_id)->value('reply_id');
            if(!$reply_id){
                Db::table('up_institutional_activities_answer')->where('reply_information_answer_id',$answer_id)->delete();
            }
            $where1['data_id'] = $answer_id;
            $where1['type'] = 1;
            $dz_id =Db::table('up_dz')->where($where1)->value('dz_id');
            if(!$dz_id){
                Db::table('up_dz')->where($where1)->delete();
            }
            if($res){
                Db::commit();
                $this->apiSuccess();
            }else{
                Db::rollback();
                $this->apiError('E001');
            }
        }elseif ($type==2){//评论回复删除
            //查询评论的发布人
            $reply_user_id = Db::table('up_institutional_activities_answer')->where('reply_id',$answer_id)->value('reply_user_id');
            if($reply_user_id!=$user_id){//如果不是同一个用户不能删除
                $this->apiError('E134');
            }
            $res = Db::table('up_institutional_activities_answer')->where('reply_id',$answer_id)->delete();
            $where1['data_id'] = $answer_id;
            $where1['type'] = 2;
            $dz_id =Db::table('up_dz')->where($where1)->value('dz_id');
            if(!$dz_id){
                Db::table('up_dz')->where($where1)->delete();
            }
            if($res){
                Db::commit();
                $this->apiSuccess();
            }else{
                Db::rollback();
                $this->apiError('E001');
            }
        }else{
            $this->apiError('E908');
        }
    }

    /***
     * 志愿者之星
     */
    public function Volunteer(){
        $list = Db::table('up_volunteer')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
            ->join('up_mechanism um','um.mechanism_id=uuu.user_to_mechanism_id')
            ->field('uuu.user_id,uuu.user_username,uuu.user_image,um.mechanism_intro,uuu.user_id')
            ->limit(3)
            ->order('a.volunteer_sort asc')
            ->select();
        return  $this->apiSuccess($list);
    }

    /***
     *更多志愿者
     */
    public function MoreVolunteer(){
        $page = input('param.page')?input('param.page'):1;
        $num = $page-1;
        $pageSize = 10;
        $list = Db::table('up_volunteer')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
            ->field('uuu.user_username,uuu.user_image,uuu.user_to_mechanism_id,a.*')
            ->limit($num*$pageSize,$pageSize)
            ->order('a.volunteer_sort asc')
            ->select();
        foreach ($list as $key=>&$vo){
            $res = Db::table('up_institutional_activities_member')->alias('b')
                        ->join('up_institutional_activities uia','uia.id = b.institutional_activities_id')
                        ->where('b.user_id',$vo['user_id'])
                        ->column('activity_name');
               $vo['activity'] = implode(',',$res);
            //查询自己属于哪个机构的
            $mechanism = Db::table('up_mechanism')->where('mechanism_id',$vo['user_to_mechanism_id'])->value('mechanism_name');
            $vo['mechanism'] = $mechanism;
        }
        $this->apiSuccess($list);
    }

    /***
     * 志愿者之星资料
     */
    public function VolunteerList(){
        $userModel = new  UserModel();
        $InstitutionalActivitiesModel = new InstitutionalActivitiesModel();
        $list = array();
        $user_id = input('param.uid')?input('param.uid'):51;
        $page = input('param.page')?input('param.page'):1;
        //查询自己参加的哪些活动
        $join_activities = Db::table('up_institutional_activities_member')->where('user_id',$user_id)->column('institutional_activities_id');
        $whereMap['a.id'] = array('IN',$join_activities);
        $join_list = $InstitutionalActivitiesModel->selectList($whereMap,'desc',$page);
        //用户信息
        $userList = $userModel->selectUserInfo($user_id);
        //查询用户基本信息
        $user_info = Db::table('up_u_user')->alias('a')
                            ->join('up_area ua','ua.district_id=a.user_sheng','left')
                            ->join('up_area ua1','ua1.district_id=a.user_shi','left')
                            ->join('up_area ua2','ua2.district_id=a.user_qu','left')
                            ->join('up_mechanism um','um.mechanism_id=a.user_to_mechanism_id','left')
                            ->field('a.user_rel_name,um.mechanism_name,ua.district as sheng,ua1.district as shi
                                ,ua2.district as qu')
                            ->where('a.user_id',$user_id)
                            ->find();
        //获得的荣誉
        $welfareInfo = Db::table('up_volunteer')->where('user_id',$user_id)->value('content');
        $where['sign_time'] =array('>',0);
        $where['sign_end_time'] =array('>',0);
        $where['user_id'] =$user_id;

        //参加的活动
        $activities_member = Db::table('up_institutional_activities_member')
                        ->alias('a')
                        ->join('up_institutional_activities uia','uia.id = a.institutional_activities_id')
                        ->where($where)
                        ->field('uia.activity_time,a.*')
                        ->select();
        $total_time = '';
        foreach ($activities_member as $key=>$vo){
            if(strtotime($vo['sign_end_time'])>0){
                $time = strtotime($vo['sign_end_time']) - strtotime($vo['sign_time']);
                if(floor($time/3600) > $vo['activity_time']){//如果签到的时长大于活动的举办时长
                    $total_time += $vo['activity_time'];
                }else{
                    $total_time += floor($time/3600);
                }
            }

        }
        $num = count($activities_member);//参加的活动次数
        $list['welfare'] = $welfareInfo;//荣誉
        $list['hospital'] = $this->getUserType(1,$userList['user_hospital']);//执业医院
        $list['department'] = $this->getUserType(2,$userList['user_department']);//科室
        $list['title'] = $this->getUserType(3,$userList['user_title']);//职称
        $list['post'] = $this->getUserType(4,$userList['user_post']);//职务
        $list['num'] = $num;//参加活动的次数
        $list['time'] = $total_time;//参加活动的时间
        $list['user'] = $user_info;//基本资料
        $list['img'] = $userList['user_business_image'];
        $list['join'] = $join_list;
        $this->apiSuccess($list);
    }

    /***
     * @param $type
     * @param $mold
     * @return mixed
     * 查询自己的信息
     */
    protected function getUserType($type,$mold){
        $where['assortment_type'] = $type;
        $where['assortment_id'] = $mold;
        $assortment_name = Db::table('up_assortment')->where($where)->value('assortment_name');
        return $assortment_name;
    }

    /***
     * 热门回答
     */
    public function HotAnswer()
    {
        $now_time = date('Y-m-d H:i:s',time());
        $last_time = date('Y-m-d H:i:s',strtotime('-1 week'));
        $where['interlocution_time'] =array('between',array($last_time,$now_time));
        $list = Db::table('up_interlocution')->where($where)->order('interlocution_num desc')->limit(5)->select();
        foreach($list as $key=>&$vo){
            $res = Db::table('up_interlocution_answer')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
                ->where('f_interlocution_id',$vo['interlocution_id'])
                ->field('user_username,user_image')
                ->order('answer_time desc')
                ->find();
            $vo['user_username'] =$res['user_username'];
            $vo['user_image'] =$res['user_image'];
        }
        $this->apiSuccess($list);
    }

    /**
     * 回答列表
     */
    public function Answer(){
        $type = input('param.type')?input('param.type'):1;
        $page = input('param.page')?input('param.page'):1;
        $num = $page-1;
        if($type==1){
            $now_time = date('Y-m-d H:i:s',time());
            $last_time = date('Y-m-d H:i:s',strtotime('-1 week'));
            $where['a.interlocution_time'] =array('between',array($last_time,$now_time));
            $order = 'interlocution_num desc';
        }elseif ($type==2){
            $order = 'interlocution_time desc';
            $where = 1;
        }

        $list = Db::table('up_interlocution')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
            ->where($where)
            ->order($order)
            ->limit($num*10,10)
            ->field('uuu.user_username,uuu.user_image,a.*')
            ->select();
        foreach ($list as $Key=>&$vo){
            $vo['interlocution_img'] = trim($vo['interlocution_img'],',');

//            $day = date('Y-m-d',time());//当前时间
//            $time = time() - 3600*24;//昨天的时间戳
//            $last_time = date('Y-m-d 00:00:00',$time);//昨天开始
//            $last_time1 = date('Y-m-d 23:59:59',$time);//昨天结束
//
//            if($day==date('Y-m-d',strtotime($vo['interlocution_time']))){//判断是否是今天
//                $vo['interlocution_time'] =date('H:i',strtotime($vo['interlocution_time']));
//                $vo['time_name'] ='今天';
//            }
//           if($last_time<$vo['interlocution_time']&&$vo['interlocution_time']<$last_time1){//判断是否是昨天
//               $vo['interlocution_time'] =date('m-d H:i',strtotime($vo['interlocution_time']));
//                $vo['time_name'] = '昨天';
//           }
//           $count = Db::table('up_interlocution_answer')->where('f_interlocution_id',$vo['interlocution_id'])->count();
//            $vo['num'] = $count?$count:0;
        }
//        if($list){
            $this->apiSuccess($list);
//        }else{
//            $this->apiError('E906');
//        }
    }

    /***
     * 回答列表详情
     */
    public function AnswerDetail(){
        extract(input());

        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
            $page = input('param.page')?input('param.page'):1;
            $num = $page-1;
            $id = input('param.interlocution_id')?input('param.interlocution_id'):20;
        $list = Db::table('up_interlocution')->alias('ui')
            ->join('up_u_user uuu','uuu.user_id = ui.user_id','left')
            ->where('interlocution_id',$id)->field('ui.*,uuu.user_username')->find();
//        $list['interlocution_img'] = explode(',',trim($list['interlocution_img'],','));
        $aaa = explode(',',trim($list['interlocution_img'],','));
        if(count(array_filter($aaa))>0){
            $list['interlocution_img'] =$aaa;
        }else{
            $list['interlocution_img'] =array();
        }
            $res = Db::table('up_interlocution_answer')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
                ->where('f_interlocution_id',$id)
                ->limit($num*10,10)
                ->field('a.*,uuu.user_username as username,uuu.user_image')
                ->order('a.answer_time desc')
                ->select();
            foreach ($res as $key=>&$vo){
                $aa = explode(',',trim($vo['answer_img'],','));
                if(count(array_filter($aa))>0){
                    $vo['answer_img'] =$aa;
                }else{
                    $vo['answer_img'] =array();
                }
                $where['type'] = 5;
                $where['data_id'] = $vo['answer_id'];
                $where['user_id'] = $user_id;
                $dz_id = Db::table('up_dz')->where($where)->value('dz_id');
                if($dz_id){//点赞
                    $vo['dz'] = 1;
                }else{
                    $vo['dz'] = 2;//没有点赞
                }
            }
            $list['children'] = $res;
        $this->apiSuccess($list);
    }

    /***
     * 提问
     */
    public function PutQuestions(){
        extract(input());

        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
            $content = input('param.content');
            $title = input('param.title');
            $img = input('param.img');
            $data['user_id'] = $user_id;
            $data['interlocution_time'] = date('Y-m-d H:i:s',time());
            $data['interlocution_img'] = $img;
            $data['interlocution_title'] = $title;
            $data['interlocution_introduce'] = $content;
            Db::table('up_interlocution')->insertGetId($data);
            $this->apiSuccess();
    }
    /***
     * 问答评论提交
     */
    public function AnswerComment(){
        extract(input());

        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
        $data['f_interlocution_id'] = input('param.interlocution_id');
        $data['answer_content']= input('param.content');
        $data['answer_img'] = input('param.img');
        $data['answer_time'] = date('Y-m-d H:i:s',time());
        $data['user_id'] = $user_id;
        $res = Db::table('up_interlocution_answer')->insertGetId($data);
        Db::table('up_interlocution')->where('interlocution_id',input('param.interlocution_id'))->setInc('interlocution_num',1);
        if($res){
            $this->apiSuccess();
        }else{
            $this->apiError('E000');

        }
    }

    /***
     * 编辑问题
     */
    public function updateAnswer(){
        extract(input());

        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
        $id = input('param.id');
        $aa = Db::table('up_interlocution')->where('interlocution_id',$id)->value('user_id');
        if($aa!=$user_id){
            $this->apiError(array('msg' => "不能编辑不属于自己的问题", 'status' => 'L201'));
        }
        $content = input('param.content');
        $title = input('param.title');
        $img = input('param.img');
        $data['user_id'] = $user_id;
        $data['interlocution_time'] = date('Y-m-d H:i:s',time());
        $data['interlocution_img'] = $img;
        $data['interlocution_title'] = $title;
        $data['interlocution_introduce'] = $content;
         Db::table('up_interlocution')->where('interlocution_id',$id)->update($data);
        $this->apiSuccess();
    }
    /***
     * 问答评论点赞  取消点赞
     */
    public function AnswerDz(){
        extract(input());

        $id = input('param.answer_id');
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
        $where['type'] = 5;
        $where['data_id'] = $id;
        $where['user_id'] = $user_id;
        $dz = Db::table('up_dz')->where($where)->value('dz_id');
        if($dz){//证明已经点赞就取消点赞
            $res = Db::table('up_interlocution_answer')->where('answer_id',$id)->setDec('answer_zang',1);
            $list = Db::table('up_dz')->where($where)->delete();
        }else{//点赞
            $res = Db::table('up_interlocution_answer')->where('answer_id',$id)->setInc('answer_zang',1);
            $data['user_id'] = $user_id;
            $data['type'] = 5;
            $data['data_id'] = $id;
            $list = Db::table('up_dz')->insertGetId($data);
        }
        if($list&&$res){
            $this->apiSuccess();
        }else{
            $this->apiError('E133');
        }
    }
    /***
     * 问答删除  问答回复删除
     */
    public function AnswerDelete(){
        extract(input());

        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
        $type = input('param.type');
        $id = input('param.id');
        if($type==1){//删除的是问答
            $res = Db::table('up_interlocution')->where('interlocution_id',$id)->find();
            if($res['user_id']!=$user_id){
                $this->apiError('E134');
            }else{
                $list = Db::table('up_interlocution')->where('interlocution_id',$id)->delete();
                $answer_id = Db::table('up_interlocution_answer')->where('f_interlocution_id',$id)->value('answer_id');
                if(!$answer_id){
                    Db::table('up_interlocution_answer')->where('f_interlocution_id',$id)->delete();
                }
                if($list){
                    $this->apiSuccess();
                }else{
                    $this->apiError('E001');
                }
            }
        }elseif ($type==2){
            //删除的是问答评论
            $res = Db::table('up_interlocution_answer')->where('answer_id',$id)->find();
            if($res['user_id']!=$user_id){
                $this->apiError('E134');
            }else{
                $list = Db::table('up_interlocution_answer')->where('answer_id',$id)->delete();
                Db::table('up_interlocution')->where('interlocution_id',$res['f_interlocution_id'])->setDec('interlocution_num',1);

                if($list){
                    $this->apiSuccess();
                }else{
                    $this->apiError('E001');
                }
            }
        }
    }

    /**
     * 消息数量
     */
    public function message(){
        extract(input());

        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
        $uid = ','.$user_id.',';
        $where['n_user_id'] = array('like',"%$uid%");
        $where['n_read_user'] = array('notlike',"%$uid%");
        $where['n_clear_user'] = array('notlike',"%$uid%");
        $where['n_status'] = 1;

        $count  = Db::table('up_notice')->where($where)->count();
        if($count > 99){
            $count = '99+';
        }
        $data['num'] = $count;
        $this->apiSuccess($data);
    }

    /***
     * 消息列表
     */
    public function messageList(){
        extract(input());

        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
        $uid = ','.$user_id.',';
        $page = input('param.page')?input('param.page'):1;
        $where['n_user_id'] = array('like',"%$uid%");
        $where['n_clear_user'] = array('notlike',"%$uid%");
        $where['n_status'] = 1;

        $data  = Db::table('up_notice')->where($where)
            ->field('n_createtime,n_id,n_title,n_read_user,n_type,n_activitie,n_img,n_status')
            ->order('n_createtime desc')
            ->limit(($page-1)*10,10)->select();
        foreach ($data as $key=>&$vo){
            $whereMap['n_id'] = $vo['n_id'];
            $whereMap['n_read_user'] = array('like',"%$uid%");
            $n_id  = Db::table('up_notice')->where($whereMap)->value('n_id');
            if($n_id){
                $vo['read'] = 1;//已读
            }else{
                $vo['read'] = 2;//未读
            }
            if($vo['n_type']!=1){
                $activity_img = Db::table('up_institutional_activities')->where('id',$vo['n_activitie'])->value('activity_img');
                $num = count($activity_img);
                $list = explode(',',$activity_img);
                $vo['n_img'] =$list[$num-$num] ;
            }
        }
        return json_encode(['status' => 200, 'msg' => '成功', 'data' => $data],true);
    }

    public function messageDetail(){
        extract(input());

        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
        $n_id = input('param.n_id');
        $list   = Db::table('up_notice')->where('n_id',$n_id)->field('n_detail')->find();
        return json_encode(['status' => 200, 'msg' => '成功', 'data' => $list],true);
    }

    /***
     * 消息删除
     */
    public function messageDetele(){
        extract(input());

        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
        $aid = input('param.n_id');
        $users = Db::table('up_notice')->where('n_id',$aid)->value('n_clear_user');
        if($users){
            $str = $users . $user_id . ',';
        }else{
            $str = ",$user_id,";
        }
        $res = Db::table('up_notice')->where('n_id',$aid)->update(['n_clear_user'=>$str]);
        if($res){
            return json_encode(['status'=>200,'msg'=>'成功']);
        }else{
            return json_encode(['status'=>0,'msg'=>'失败']);
        }
    }

    /***
     * 消息已读
     */
    public function readmessage(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
        $aid = input('param.n_id');
        $n_read_user = Db::table('up_notice')->where('n_id',$aid)->value('n_read_user');
        $uid = ','.$user_id.',';
        if(strpos($n_read_user,$uid) ===false){
            $str = $n_read_user.$user_id.',';
            //更新发送
            Db::table('up_notice')->where('n_id',$aid)->setField('n_read_user',$str);
            $this->apiSuccess();
        }else{
            return json(array('status'=>201));
        }
    }
}